package com.iotstudio.embeddedcloudplatform.controller;

import com.alibaba.fastjson.JSONArray;
import com.iotstudio.embeddedcloudplatform.POJO.DO.SensorData;
import com.iotstudio.embeddedcloudplatform.POJO.DO.UserSensor;
import com.iotstudio.embeddedcloudplatform.POJO.VO.ResultVO;
import com.iotstudio.embeddedcloudplatform.constants.CommonConstants;
import com.iotstudio.embeddedcloudplatform.constants.HttpParamKey;
import com.iotstudio.embeddedcloudplatform.service.SensorService;
import com.iotstudio.embeddedcloudplatform.service.serviceImp.UserServiceImp;
import com.iotstudio.embeddedcloudplatform.util.KeyUtil;
import com.iotstudio.embeddedcloudplatform.util.page.ListUtil;
import com.iotstudio.embeddedcloudplatform.util.web.CookieUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 传感器操作
 * @author BeFondOfTaro
 * Created at 12:12 2018/1/20
 */
@RestController
@Api(tags = "传感器操作")
public class SensorController {
    @Autowired
    private SensorService sensorService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /*@ApiImplicitParams({
            @ApiImplicitParam(name = HttpParamKey.CLIENT_DIGEST,value = "令牌",paramType = "header",required = true),
    })
    @PostMapping(CommonConstants.PUB_PREFIX + "/Sensor")
    public ResultVO addSensor(
            @Param("name") String name,
            @Param("tag") String tag,
            @Param("is_share") Boolean isShare
    ){
        Sensor sensor=new Sensor(name,tag,isShare);
        SensorDataType sensorDataType=new SensorDataType(tag);
        return sensorService.add(sensor,sensorDataType);
    }*/

    /**
     * 传输传感器数据
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = HttpParamKey.CLIENT_DIGEST,value = "令牌",paramType = "header",required = true),
    })
    @PostMapping(CommonConstants.PUB_PREFIX + "/Data")
    public ResultVO dataTransmission(
            @Param("sensorId") String sensorId,
            @Param("value") BigDecimal value,
            @Param("name") String name
    ){
        SensorData sensorData=new SensorData(sensorId,value);
        return sensorService.dataTransmission(sensorData,name);
    }

    /**
     * 删除传感器
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = HttpParamKey.CLIENT_DIGEST,value = "令牌",paramType = "header",required = true),
    })
    @PostMapping(CommonConstants.PUB_PREFIX + "/deleteSensor")
    public ResultVO deleteSensor(
            @Param("id") String id,
            @Param("name") String name
    ){
        return sensorService.deleteSensor(id,name);
    }





    /**
     * 添加传感器
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = HttpParamKey.CLIENT_DIGEST,value = "令牌",paramType = "header",required = true),
    })
    @PostMapping(CommonConstants.PUB_PREFIX + "/Sensor")
    public ModelAndView addSensor(
            @Param("sensorId") String sensorId
    ){
        System.out.println(sensorId);
        String id = KeyUtil.genUniqueKey();
        String userId=redisTemplate.opsForValue().get("CURRENT_USER");
        UserSensor userSensor=new UserSensor(id,userId,sensorId);
        return sensorService.add(userSensor);
    }

    /**
     * 展示传感器数据
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = HttpParamKey.CLIENT_DIGEST,value = "令牌",paramType = "header",required = true),
    })
    @GetMapping(CommonConstants.PUB_PREFIX + "/showData")
    public Object showData(
            //@Param("sensorId") String sensorId
            HttpServletRequest request
    ){
        String sensorId = CookieUtil.getCookieValueByName(request,"sensorId");
        System.out.println(sensorId);
        return sensorService.showSensorData(sensorId);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = HttpParamKey.CLIENT_DIGEST,value = "令牌",paramType = "header",required = true),
    })
    @GetMapping(CommonConstants.PUB_PREFIX + "/index")
    public ModelAndView index(
    ){
        return new ModelAndView("redirect:/static/login.html");
    }

}
